package com.biz.primus.ms.order.dao.repository;

import com.biz.primus.model.order.enums.PaymentState;
import com.biz.primus.ms.base.jpa.repository.CommonJpaRepository;
import com.biz.primus.ms.order.dao.po.Order;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;

/**
 * 订单资源接口
 *
 * @author Tiger Feng
 * @date 2017年10月30日
 */
@Repository
public interface OrderRepository extends CommonJpaRepository<Order, Long>, OrderDao {

    /**
     * 根据订单编号获取订单信息
     *
     * @param orderCode
     * @return
     */
    Order findByOrderCode(String orderCode);

    /**
     * 计算今天完成的订单数量，计算条件：支付状态为支付成功和货到付款 and 创建时间是今天
     * @param paymentState1 支付状态：待支付
     * @param paymentState2 支付状态：货到付款
     * @param timestamp 当天凌晨0:0:0时间
     * @return
     */
    @Query("select count(o.id) from Order o where (o.paymentState=?1 or o.paymentState=?2) and ?3 <= o.createTimestamp")
    int countTodayFinish(PaymentState paymentState1, PaymentState paymentState2, Timestamp timestamp);
}
